package com.uroad.lib.widget.pulltorefresh;

import android.content.Context;
import android.util.AttributeSet;

import com.uroad.lib.R;
import com.uroad.lib.widget.pulltorefresh.header.MaterialHeader;

/**
 * used to refresh layout
 * Created by jiadong on 2016/11/9.
 */
public class MaterialPullToRefreshLayout extends PtrFrameLayout {
    private MaterialPullToRefreshLayout materialPullToRefreshLayout;

    public MaterialPullToRefreshLayout(Context context) {
        super(context);
        iniHeader(context);
    }

    public MaterialPullToRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        iniHeader(context);
    }

    public MaterialPullToRefreshLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        iniHeader(context);

    }

    private void iniHeader(Context context) {
        materialPullToRefreshLayout = this;
        final MaterialHeader header = new MaterialHeader(context);
        int[] colors = getResources().getIntArray(R.array.google_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new LayoutParams(-1, -2));
        header.setPadding(0, 15, 0, 10);
        header.setPtrFrameLayout(this);
        this.setPinContent(true);
        this.setLoadingMinTime(300);
        this.setDurationToCloseHeader(300);
        this.setHeaderView(header);
        this.addPtrUIHandler(header);

        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                materialPullToRefreshLayout.autoRefresh(true, 0);
            }
        }, 0);
        this.setEnabledNextPtrAtOnce(true);
    }
}
